package com.triangle.cashier.widget;


import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.triangle.cashier.R;
import com.triangle.cashier.tools.Screen;

/**
 * 确认对话框
 */

public class ConfirmDialog extends DialogFragment implements View.OnClickListener {

    private ConfirmResultClickListener confirmResultClickListener;
    private String title,content;


    public ConfirmDialog(String title,String content,ConfirmResultClickListener confirmResultClickListener) {
        this.title = title;
        this.content = content;
        this.confirmResultClickListener = confirmResultClickListener;

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View customView = LayoutInflater.from(getActivity()).inflate(
                R.layout.confirm_dialog, null);
        initView(customView);
        Dialog dialog = new Dialog(getActivity(), R.style.soft_para_dialog);
        dialog.setContentView(customView);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (Screen.getScreenWidth(getActivity()) * 0.5);   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        return dialog;
    }

    private void initView(View view) {
        ((TextView)view.findViewById(R.id.confirm_title)).setText(title);
        ((TextView)view.findViewById(R.id.confirm_content)).setText(content);

        ImageButton btn = (ImageButton)view.findViewById(R.id.close_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmResultClickListener.cancel();
            }
        });

        Button cancelBtn = view.findViewById(R.id.btn_cancel);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmResultClickListener.cancel();
            }
        });

        Button okBtn = view.findViewById(R.id.btn_ok);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmResultClickListener.ok();
            }
        });
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = this.getDialog().getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        window.getDecorView().setSystemUiVisibility(uiOptions);
    }

    @Override
    public void onClick(View v) {

    }

    public interface ConfirmResultClickListener {
        void cancel();
        void ok();
    }

}
